<?php
/**
 * Created by Benjiemin
 * Date: 2020/2/26
 * Time: 16:45
 */
require ('./vendor/autoload.php');
require ('./aip-php-sdk-2.2.15\AipOcr.php');

use QL\QueryList;
use QL\Ext\Chrome;

$ql = QueryList::getInstance();
// 注册插件，默认注册的方法名为: chrome
$ql->use(Chrome::class);
$text = $ql->chrome(function ($page,$browser) {
    $username = 'nn301';
    $password = 'qwe123123';

    $page->goto('http://nc1.win3008.com/member/login');
   //获取页面截图
    $page->screenshot([
        'path' => 'verify.jpeg',
        'fullPage' => true
    ]);

    $filename = 'verify.jpeg';
    //验证码识别
    $appId = '9360309';
    $apiKey = 'f94FyaKX2LKx0GWBoddFVpxa';
    $secretKey = 'oGhM9QqHH6qslErF1LH8etP6Ber5adDz';
    $apiOcrModel = new AipOcr($appId, $apiKey, $secretKey);
    $verifyRet = $apiOcrModel->basicGeneral( file_get_contents($filename ));

    if(!isset($verifyRet['words_result'][5]['words'])){
        echo '识别验证码失败';die;
    }
    $validateCode =  $verifyRet['words_result'][5]['words'];

    // 这里故意设置一个很长的延长时间，让你可以看到chrome浏览器的启动
    $page->type("input[name='loginName']",$username);
    $page->type("input[name='loginPwd']",$password);
    $page->type("input[name='ValidateCode']",$validateCode);

    $page->click("input[name='Submit']");
    //登录
    $html = $page->content();
    sleep(1);
    //点击确认
    $page->click("#Submit");
    //点击新闻
    $page->goto('http://nc1.win3008.com/news/text/0?0.21222746993288832');
    //回车
    $page->mouse->click(640, 270);
    $page->keyboard->press('Enter');
    sleep(3);
    //获取cookies
    $cookies = $page->cookies('http://nc1.win3008.com/member/');

    $PHPSESSID = $cookies[1]['value'];
    $SERVER_ID = $cookies[0]['value'];

    echo    $PHPSESSID.PHP_EOL.$SERVER_ID.PHP_EOL;

    //获取drawid
    $res = QueryList::get('http://nc1.win3008.com/drawresult8/drawresult/0.20956551265644063', [
    ],
        [
            'timeout' => 30,
            'headers' => [
                'Referer' => 'http://nc1.win3008.com/lottery/pt8/css/ball.css',
                'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
                'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'Accept-Encoding'      => 'gzip, deflate',
                'Upgrade-Insecure-Requests'=>'1',
                'Origin'=>'http://nc1.win3008.com',
                'Host'=>'nc1.win3008.com',
             //   'Cookie'    => 'PHPSESSID=lhk2cvlp1g8lhbvd8pi9ool367; SERVER_ID=vserver-il2yglvh-backend-ap13syzz; betquick=0; GameTypeLann=8'
                'Cookie'    => "PHPSESSID={$PHPSESSID}; SERVER_ID={$SERVER_ID}; betquick=0; GameTypeLann=8"
            ]
        ]
    );
    $issueJson = $res->getHtml();
    $drawid = trim(substr($issueJson,9,6));
    //投注
    $res = QueryList::post('http://nc1.win3008.com/member/bet/8', [
        'draw_ID' => ($drawid+1),
        'p_ID'=>'8101,8111',
        'p_Odds' => '1.982,1.982',
        'bet_Money' => '2,2',
        'checksum' => mt_rand(10000,99999),
        'ms_1' => '50',
        'ms_2' => '100',
        'ms_3' => '200',
        'ms_4' => '500',
        'ms_5' => '1000',
        'ms_6' => '5000',
    ],
        [
            'timeout' => 30,
            'headers' => [
                'Referer' => 'http://nc1.win3008.com/lottery/pt8/css/ball.css',
                'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
                'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'Accept-Encoding'      => 'gzip, deflate',
                'Upgrade-Insecure-Requests'=>'1',
                'Origin'=>'http://nc1.win3008.com',
                'Host'=>'nc1.win3008.com',
                'Cookie'    =>"PHPSESSID={$PHPSESSID}; SERVER_ID={$SERVER_ID}; betquick=0; GameTypeLann=8"
            ]
        ]
    );

    file_put_contents('ret.html',$res->getHtml());
    $browser->close();
    sleep(5);
    // 返回值一定要是页面的HTML内容
    return $html;
},[
    'headless' => false, // 启动可视化Chrome浏览器,方便调试
    'devtools' => false, // 打开浏览器的开发者工具
])->find('title')->text();
